<?php
require '../$header.php';
require '../$enforce_login.php';

$return = array();
$error = false;

$SpotID = $DB->escape($_POST['SpotID']);
$Message = $DB->escape($_POST['Message']);

$return = array();

if(!$error)
{
	if(strlen($Message) == 0)
	{
		$return['Error'] = "Message field is empty";
		$error = true;
	}
}

if(!$error)
{
	$DB->query("
		INSERT INTO message
		(
			SpotID,
			UserID,
			Message
		)
		VALUES
		(
			'{$SpotID}',
			'{$UserID}',
			'{$Message}'
		)
	");
	$MessageID = $DB->getInsertID();
	
	$DB->query("
		SELECT m.*, u.FirstName, u.LastName, u.Email
		FROM message AS m
		INNER JOIN user AS u
		ON u.ID = m.UserID
		WHERE m.ID = '{$MessageID}'
	");
	
	if($DB->num() == 1)
	{
		$fetch = $DB->fetch();
		$return = $fetch;
	}
	else
	{
		$return['Error'] = "Could not find message!";
	}
}

echo json_encode($return);

require '../$footer.php';
?>